import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/handle/xindong_user_handle.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/manager/xindong_shengyin_zhendong_singleton.dart';
import 'package:xindong/models/xindong_push_class.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/container/xindong_fenzu_container.dart';
import 'package:xindong/widgets/xindong_common_switch_item.dart';

class XinDongSettingPushYeMian extends StatefulWidget {
  const XinDongSettingPushYeMian({Key? key}) : super(key: key);

  @override
  State<XinDongSettingPushYeMian> createState() => _XinDongSettingPushYeMianState();
}

class _XinDongSettingPushYeMianState extends State<XinDongSettingPushYeMian> {
  final _userHandle = XinDongUserHandle();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      // 拉取信息
      XinDongUserController.to.requestPushList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("新消息通知"),
      body: GetBuilder<XinDongUserController>(builder: (controller) {
        // 服务端数据
        List<String>? titleList = controller.pushList?.map((e) => e.title).toList();
        final dataList = controller.pushList?.map((e) {
          XinDongPushClass model = e;
          return model.child?.map((e) => e).toList();
        }).toList();
        // 本地数据
        final voiceVibrateManager = XinDongShengYinZhenDongSingleton.instance;
        titleList?.add("APP打开时");
        dataList?.add([
          XinDongPushClass()
            ..tid = -100
            ..title = "声音"
            ..enable = voiceVibrateManager.enableVoice ? 1 : 0,
          XinDongPushClass()
            ..tid = -101
            ..title = "震动"
            ..enable = voiceVibrateManager.enableVibrate ? 1 : 0,
        ]);
        return XinDongGroupSectionContainer(
          dataList: dataList,
          itemBuilder: (BuildContext context, int section, int index, data) {
            XinDongPushClass model = data;
            return XinDongCommonSwitchItem(
              title: model.title,
              value: model.enable == 1,
              onChanged: (open) async {
                if (model.tid > 0) {
                  final value = open ? 1 : 0;
                  final result = await _userHandle.userPushEdit(id: model.id);
                  if (result) {
                    model.enable = value;
                    controller.update();
                  }
                } else if (model.tid == -100) {
                  voiceVibrateManager.changeEnableVoice();
                  controller.update();
                } else if (model.tid == -101) {
                  voiceVibrateManager.changeEnableVibrate();
                  controller.update();
                }
              },
            );
          },
          sectionHeaderBuilder: (BuildContext context, int index) {
            return Container(
              height: 40,
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.only(left: kPaddingValue),
              child: Text(
                titleList![index],
                style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
              ),
            );
          },
          separatorBuilder: (BuildContext context, int index) => const SizedBox(),
        );
      }),
    );
  }
}
